package sa4

import javax.servlet.http._
import scala.xml._
import scala.collection.JavaConversions._
import scala.util.matching.Regex
import javax.servlet.ServletConfig
import scala.collection.mutable.HashMap
import scala.xml.transform._
import scala.xml.Null // hide scala.Null
import sa4.routing._
import sa4.Persistence._
import sa4.sessions._
import org.squeryl.SessionFactory
import org.squeryl.Session
import org.squeryl.adapters.H2Adapter
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.KeyedEntity
import org.squeryl.dsl.ast.LogicalBoolean
import org.squeryl.annotations.OptionType

// This is a very simple Scala servlet.
// If you rename this class, you must also change the file
// src/main/webapp/WEB-INF/web.xml to use the new name.
class SimpleServletWithForms extends HttpServlet {
  //type HandlerFunction = (HttpServletRequest, HttpServletResponse) => Unit

  override def init(config: ServletConfig) {
    //super.init(config)
    Class.forName("org.h2.Driver")
    
    SessionFactory.concreteFactory = Some(() =>
      Session.create(
        java.sql.DriverManager.getConnection("jdbc:h2:" + System.getProperty("user.dir") + "/test"),
        new H2Adapter))
    
   transaction {
     //println("HEY")
      //WallDB.drop
      //WallDB.create
    }
             
//   transaction {
//	   if (WallDB.tableNameFromClass(classOf[User]) == null) {
//		// println("CREATING")  
//	     WallDB.create
//	   }
//    } 
      
    setRoutes;
  }

  override def service(req: HttpServletRequest, resp: HttpServletResponse) {
    // first i check in the proxy if i have a chached version for the response
    if (req.getRequestURI() == "/favicon.ico") {} else {
        responseBuilder.builder(req, resp)
    }
  }
}
